import quantities
import sys
import signal
import random
import time

from pyFAMOUSO.publisher import PublisherEventChannel
from pyFAMOUSO.event import Event

from abstract_node.definition import AbstractNode
from abstract_node.output_interfaces import OutputInterfaceToFAMOUSO
from abstract_node.output_interfaces import OutputInterfaceToConsole
from abstract_node.output_layer import AbstractOutputLayer

def sig_handler(signum, frame):
    global MyAbstractNode
    MyNode.stop_node()
    print "\nAus Maus"    
    sys.exit(1)
  
class MyOutputInterface(OutputInterfaceToConsole):
    
    def __init__(self, eventDataSheet):
        OutputInterfaceToConsole.__init__(self, eventDataSheet)
        
    def output(self,event):
        value=event.get_value('Measurement')
        if value>70:
            print "--> My Output Interface - Value greater than 70!"
        
class MyAbstractNode(AbstractNode):
    
    def __init__(self, eventDataSheet, outputLayer, appName):
        AbstractNode.__init__(self, eventDataSheet=eventDataSheet, 
                              outputLayer=outputLayer, 
                              appName=appName)
        
    def processing_ctr(self):
        results=[]
        return results
    
    def evaluation_fct(self):
        common_validity=1
        return common_validity
    
    def output_event_mapping(self, result, common_validity):
        limits=[0, 100]
        meas=random.randint(limits[0], limits[1])
        self.output_event.set_value(meas*quantities.centimeter, 'Measurement')
        self.output_event.set_value(5.0*quantities.second, 'Timestamp')
        self.output_event.set_value([0.5]*quantities.centimeter**2, 'Uncertainty')
        self.output_event.set_value([100, 200, 0]*quantities.centimeter, 'Position')
        self.output_event.set_value([100, 200, 0]*quantities.degree, 'Orientation') 
        self.output_event.set_value([common_validity], 'Validity')        


if __name__ == "__main__": 
       
    print "-----------------------------------------------------"    
    print "Test of an event driven Abstract Node"
    print "Two output functions are hooked for different devices:"
    print "  * Console"
    print "  * FAMOUSO Network"
    print "  * User defined output device"
    print "-----------------------------------------------------"
    
    print "\n--> Read data sheet ..."
    data_sheet='../datasheets/events/'+"Dist_IRt.xml"
    
    # Output Definition of the Abstract Node
    MyNetworkOutput=OutputInterfaceToFAMOUSO(data_sheet)
    MyConsolOutput=OutputInterfaceToConsole(data_sheet)
    MyIndividualOutput=MyOutputInterface(data_sheet)
    
    MyAbstractOutputLayer=AbstractOutputLayer([MyNetworkOutput, 
                                               MyConsolOutput, 
                                               MyIndividualOutput])
    
    MyNode=MyAbstractNode(eventDataSheet=data_sheet,
                        outputLayer=MyAbstractOutputLayer,
                        appName="Smart Camera Sensor")
    
    print MyNode
    
    print "Mit STRG + C beenden."
    signal.signal(signal.SIGINT, sig_handler)    
    
    MyNode.start_node()
    
    while 1:
        time.sleep(0.1)
